import fly from 'flyio';
import {
    host,
    timeout,
    apiWhiteList
} from './config';
import {
    Spinner
} from 'spin.js';

//设置超时
fly.config.timeout = timeout;
//设置请求基地址
fly.config.baseURL = host;

let target = document.getElementById('loadingSpin');
let spinner = new Spinner({color:'#fff', lines: 12});

//添加请求拦截器
fly.interceptors.request.use((request) => {
    target.style.display = 'block';
    spinner.spin(target)
    return request;
})

//添加响应拦截器，响应拦截器会在then/catch处理之前执行
fly.interceptors.response.use(
    (response) => {
        //只将请求结果的data字段返回
        spinner.spin();
        target.style.display = 'none';
        return response.data
    },
    (err) => {
        spinner.spin();
        target.style.display = 'none';
        // alert(err.message || "网络错误");
        console.error(err);
        return Promise.reject(err);
    }
)

export default fly;